package no.uio.inf5750.j2mewsclient;

/*
 * Responsible for talking to the webservice.
 */
public class ConvertTemperatureThread extends Thread {

    private String celsius;
    private IResultReceiver resReceiver;

    public void convert(String celsius, IResultReceiver resReceiver) {
        this.celsius = celsius;
        this.resReceiver = resReceiver;
        this.start();
    }

    public void run() {

        ConvertTemperatureSoap_Stub ct = new ConvertTemperatureSoap_Stub();
        TemperatureUnit fU = new TemperatureUnit("degreeCelsius");
        TemperatureUnit tU = new TemperatureUnit("degreeFahrenheit");

        try {
            // Use webservice to convert temperature
            String fahrenheit = ct.convertTemp(celsius, fU, tU);

            // Return result to GUI
            resReceiver.addResult(celsius, fahrenheit);

        } catch (Exception e) {
            // Return error to GUI
            resReceiver.addResult(celsius, new Exception("Failed to communicate with webservice."));
        }
    }

}
